package com.poly.system.controller;

import com.poly.common.core.domain.R;
import com.poly.common.core.validate.Add;
import com.poly.common.core.validate.Update;
import com.poly.common.core.web.controller.BaseController;
import com.poly.common.log.annotation.Log;
import com.poly.common.log.enums.BusinessType;
import com.poly.common.security.annotation.RequiresPermissions;
import com.poly.system.domain.SysAppMenu;
import com.poly.system.pojo.dto.SysAppMenuDTO;
import com.poly.system.pojo.query.SysAppMenuQuery;
import com.poly.system.pojo.vo.SysAppMenuVO;
import com.poly.system.service.ISysAppMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 应用菜单Controller
 *
 * @author poly
 * @date 2024-01-02
 */
@Api(tags = "应用菜单")
@RestController
@RequestMapping("/appmenu")
public class SysAppMenuController extends BaseController
{
    @Autowired
    private ISysAppMenuService appMenuService;

    /**
     * 查询应用菜单列表
     */
    @ApiOperation("查询应用菜单列表")
    @RequiresPermissions("app:menu:list")
    @GetMapping("/list")
    public R<List<SysAppMenuVO>> list(SysAppMenuQuery query)
    {
        return R.ok(appMenuService.list(query));
    }

    /**
     * 获取应用菜单详细信息
     */
    @ApiOperation("获取应用菜单详细信息")
    @RequiresPermissions("app:menu:query")
    @GetMapping(value = "/{id}")
    public R<SysAppMenu> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(appMenuService.getById(id));
    }

    /**
     * 新增应用菜单
     */
    @ApiOperation("新增应用菜单")
    @Log(title = "应用菜单管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("app:menu:add")
    @PostMapping
    public R add(@RequestBody @Validated({Add.class}) SysAppMenuDTO dto) {
        return R.ok(appMenuService.add(dto));
    }

    /**
     * 修改应用菜单
     */
    @ApiOperation("修改应用菜单")
    @Log(title = "应用菜单管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("app:menu:edit")
    @PutMapping
    public R edit(@RequestBody @Validated({Update.class}) SysAppMenuDTO dto) {
        if(appMenuService.update(dto)){
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 删除应用菜单
     */
    @ApiOperation("删除应用菜单")
    @Log(title = "应用菜单管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("app:menu:remove")
	@DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids)
    {
        appMenuService.delete(Arrays.asList(ids));
        return R.ok();
    }
}
